package 西交915编程题._2019.第三题;

import java.util.Arrays;

public class Question3 {
    //输出数组中的所有逆序对   分治
    //归并排序
    void partition(int[] nums, int l, int r) {
        if (l < r) {
            int mid = (l + r) / 2;
            partition(nums, 0, mid);
            partition(nums, mid + 1, r);
            merge(nums, l, mid, r);
        }
    }

    private void merge(int[] nums, int l, int mid, int r) {
        int[] t = new int[r - l + 1];
        int i = l, j = mid + 1, k = 0;
        while (i <= mid && j <= r) {
            if (nums[i] > nums[j]) {
                t[k++] = nums[j++];
            } else {
                t[k++] = nums[i++];
            }
        }
        while (i <= mid) t[k++] = nums[i++];
        while (j <= r) t[k++] = nums[j++];
        System.arraycopy(t, 0, nums, l, t.length);
    }

    public static void main(String[] args) {
        int[] nums = new int[]{3, 1, 4, 5, 2};
        new Question3().partition(nums, 0, nums.length - 1);
        System.out.println(Arrays.toString(nums));

    }
}
